// pages/mine/message/message.js
const app = getApp();
const time = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    system_page: 1,
    sess_page: 1,
    sess_messages: [],
    messages: [],
    system_messages: [],
    role: app.register_info.register_role,

    
    //切换
    currentData: 0,
  },
  //去详情
  go_detail(e) {
    // console.log(e)
    let chat_id = e.currentTarget.dataset.chat_id, title = e.currentTarget.dataset.title;
    this.setData({
      index: -1
    })
    wx.navigateTo({
      url: '../../recruit/session/index?chat_id=' + chat_id +'&title=' + title
    })
  },
  delete(chat_id) {
    wx.request({
      url: app.globalData.url + 'api/swop/message/info/del',
      data: {
        token: app.globalData.token,
        chat_id: chat_id,
      },
      method: 'POST',
      success: (e) => {
        console.log(e);
        this.get_sess(1)
      }
    })
  },
  //点击删除
  click_delete(e) {
    //     console.log(1111)
    let chat_id = e.currentTarget.dataset.chat_id;
    wx.showModal({
      content: '确认删除吗?',
      showCancel: true,
      confirmText: '确认',
      confirmColor: '#d0a76c',
      success: (e) => {
        if (e.confirm) {
          this.delete(chat_id)
        }
      }
    })

    this.setData({
      index: -1
    })
  },
  //左滑删除
  touchstart(e) {
    // console.log(e)
    let startX = e.changedTouches["0"].clientX;
    this.setData({
      startX: startX,
    })
  },
  touchend(e) {
    // console.log(e)
    let endX = e.changedTouches["0"].clientX,
      startX = this.data.startX,
      index = e.currentTarget.dataset.index;

    if (endX - startX < 0) {
      console.log('出现');
      this.setData({
        index: index,
      });
    } else {
      this.setData({
        index: -1,
      });
    }
  },
  stopTouchMove() {
    return false;
  },
  //加载
  sess_load() {
    // console.log("加载")
    let that = this;
    let sess_page = this.data.sess_page;
    sess_page = sess_page + 1;
    that.setData({
      sess_page: sess_page
    })
    this.sess_list();
    // console.log("update")
  },
  // 加载
  sess_list: function (e) {
    let that = this;
    let sess_page = this.data.sess_page
    that.get_sess(sess_page);
    wx.showLoading({
      icon: 'none',
    })
  },
  //约聊
  get_sess(sess_page) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/swop/message/info',
      data: {
        token: app.globalData.token,
        page: sess_page,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        //普通情况
        if (e.data.data.list.current_page > e.data.data.list.last_page) {
          // console.log("到底了");
          wx.showToast({
            title: '到底啦~',
            icon: 'none',
          })
        } else {
          // console.log("没到底");          
          if (that.data.sess_page == "1") {
            let sess_messages_old = e.data.data.list.data;
            sess_messages_old.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            })
            that.setData({
              sess_messages: sess_messages_old,
              count_total: e.data.data.count_total
            })
          } else {
            // console.log("不不不不不");
            let sess_messages = that.data.sess_messages;
            let sess_messages_old = e.data.data.list.data;
            sess_messages_old.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            })
            sess_messages.push(...sess_messages_old)
            // console.log(recruit_info);
            that.setData({
              sess_messages: sess_messages,
              count_total: e.data.data.count_total
            })
          }
        }
        // console.log('sess_page+' + that.data.sess_page)
        wx.hideLoading();
      },
    })
  },
  // 加载
  system_list: function(e) {
    let that = this;
    let system_page = this.data.system_page;
    that.get_system(system_page);
    wx.showLoading({
      icon: 'none',
    })
  },
  //系统通知
  get_system(system_page) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/message/my/list',
      data: {
        token: app.globalData.token,
        type: 2,
        page: system_page,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        // let system_messages = e.data.data.messages;
        // that.setData({
        //   system_messages: system_messages,
        // })
        //普通情况
        if (e.data.data.current_page > e.data.data.total_page) {
          // console.log("到底了");
          wx.showToast({
            title: '到底啦~',
            icon: 'none',
          })
        } else {
          // console.log("没到底");          
          if (that.data.system_page == "1") {
            let system_messages_old = e.data.data.messages;
            system_messages_old.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            })
            that.setData({
              system_messages: system_messages_old,
                 recommend_noread_total: e.data.data.recommend_noread_total,
                 system_noread_total: e.data.data.system_noread_total,
            })
          } else {
            // console.log("不不不不不");
            let system_messages = that.data.system_messages;
            let system_messages_old = e.data.data.messages;
            system_messages_old.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            })
            system_messages.push(...system_messages_old)
            // console.log(recruit_info);
            that.setData({
              system_messages: system_messages,
                 recommend_noread_total: e.data.data.recommend_noread_total,
                 system_noread_total: e.data.data.system_noread_total,
            })
          }
        }
        // console.log('system_page+' + that.data.system_page)
        wx.hideLoading();
      },
    })
  },
  //推送信息已读
  readed(id) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/message/my/read',
      data: {
        token: app.globalData.token,
        id: id,
      },
      method: 'POST',
      success: function(e) {
        console.log(e);
      },
    })
  },
  push_system_detail(e) {
    console.log(e)
    let that = this;
    let url = e.currentTarget.dataset.url;
    let role = this.data.role;
    let id = e.currentTarget.dataset.id;
    var main_id = e.currentTarget.dataset.main_id;

    var messages = this.data.messages;
    var system_messages = this.data.system_messages;
    var messagesindex = e.currentTarget.dataset.messagesindex ? e.currentTarget.dataset.messagesindex : "";
    var system_messagesindex = e.currentTarget.dataset.system_messagesindex ? e.currentTarget.dataset.system_messagesindex : "";
    console.log('main_id+' + main_id)
    if (role == 1) {
      this.readed(id);
      wx.navigateTo({
        url: '../../recruit/jobDetail/jobDetail?deatil_id=' + main_id,
      })
    } else if (role == 2) {
      this.readed(id);
      wx.navigateTo({
        url: '../../resume/singleShow/singleShow',
      })
    }

  },

  //打开
  push_detail(e) {
    console.log(e.currentTarget.dataset);
    let url = e.currentTarget.dataset.url;
    let role = this.data.role;
    let id = e.currentTarget.dataset.id;
    var main_id = e.currentTarget.dataset.main_id;

    var messages = this.data.messages;
    var system_messages = this.data.system_messages;
    var messagesindex = e.currentTarget.dataset.messagesindex ? e.currentTarget.dataset.messagesindex : "";
    var system_messagesindex = e.currentTarget.dataset.system_messagesindex ? e.currentTarget.dataset.system_messagesindex : "";

    if (messagesindex) {
      messages[messagesindex].is_read = "1";
      this.setData({
        messages: messages,
      })
    } else if (system_messagesindex) {
      system_messages[system_messagesindex].is_read = "1";
      this.setData({
        system_messages: system_messages,
      })
    }
    if (url) {
      var rcd_id = url.split('=')[1];
    }
    if (role == 1) {
      this.readed(id);
      if (rcd_id) {
        if (app.globalData.url == 'https://xct.zhibohome.net/') {
          //测试
          wx.navigateTo({
            url: '../protocol/protocol?url=https://testh5.ilivehome.net/single/recommend/recommend_anchor.html&rcd_id=' + rcd_id + '&title=推送信息',
          })
        } else {
          //正式
          wx.navigateTo({
            url: '../protocol/protocol?url=https://www.zhibohome.net/single/recommend/recommend_anchor.html&rcd_id=' + rcd_id + '&title=推送信息',
          })
        }
      } else if (main_id) {
        wx.navigateTo({
          url: '../../recruit/singleRecruit/singleRecruit?resume_id=' + main_id,
        })
      }

    } else if (role == 2) {
      this.readed(id);
      if (rcd_id) {
        if (app.globalData.url == 'https://xct.zhibohome.net/') {
          //测试
          wx.navigateTo({
            url: '../protocol/protocol?url=https://testh5.ilivehome.net/single/recommend/recommend_job.html&rcd_id=' + rcd_id + '&title=推送信息',
          })
        } else {
          //正式
          wx.navigateTo({
            url: '../protocol/protocol?url=https://www.zhibohome.net/single/recommend/recommend_job.html&rcd_id=' + rcd_id + '&title=推送信息',
          })
        }
      } else if (main_id) {
        wx.navigateTo({
          url: '../../recruit/detail/detail?deatil_id=' + main_id,
        })
      }
    }
  },
  //加载
  load() {
    // console.log("加载")
    let that = this;
    let page = this.data.page;
    page = page + 1;
    that.setData({
      page: page
    })
    this.list();
    // console.log("update")
  },
  // 加载
  list: function(e) {
    let that = this;
    let page = this.data.page
    that.get_push(page);
    wx.showLoading({
      icon: 'none',
    })
  },
  //获取推送列表
  get_push(page) {
    let that = this;
    wx.request({
      url: app.globalData.url + 'api/message/my/list',
      data: {
        token: app.globalData.token,
        type: 1,
        page: page,
      },
      method: 'POST',
      success: function(e) {
        console.log(e)
        //普通情况
        if (e.data.data.current_page > e.data.data.total_page) {
          // console.log("到底了");
          wx.showToast({
            title: '到底啦~',
            icon: 'none',
          })
        } else {
          // console.log("没到底");          
          if (that.data.page == "1") {
            let messages_old = e.data.data.messages;
            messages_old.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            })
            that.setData({
              messages: messages_old,
              
            })
          } else {
            // console.log("不不不不不");
            let messages = that.data.messages;
            let messages_old = e.data.data.messages;
            messages_old.forEach(function(value, index) {
              value.create_time = time.formatTimeTwo(value.create_time, 'M-D h:m');
            })
            messages.push(...messages_old)
            // console.log(recruit_info);
            that.setData({
              messages: messages,
            })
          }
        }
        // console.log('page+' + that.data.page)
        wx.hideLoading();
      },
    })
  },
  //切换
  //获取当前滑块的index
  bindchange: function(e) {
    const that = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换，滑块index赋值
  checkCurrent: function(e) {
    const that = this;
    // console.log(e)
    if (that.data.currentData === e.currentTarget.dataset.current) {
      return false;
    } else {

      that.setData({
        currentData: e.currentTarget.dataset.current
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      role: app.register_info.register_role
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.get_push(1);
    this.get_system(1);
    this.get_sess(1);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      messages: [],
      system_messages: [],
      sess_messages: [],
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})